<?php

class TiposController extends AppController {

    public $sl_tipos = array(
        "actions" => array("V" => "view", "E" => "edit", "D" => "delete"),
        "filters" => array(
            array("name" => 'Tipo.codigo', "label" => 'Código'),
            array("name" => 'Tipo.nombre', "label" => 'Nombre')
        ),
        "columns" => array(
            array("name" => 'Tipo.codigo', "label" => 'Código'),
            array("name" => 'Tipo.nombre', "label" => 'Nombre')
        )
    );
    public $maint_tipos = array(
        "fields" => array(
            "Descripción del Tipo" => array(
                array("name" => "fecha_carga", "label" => "Fecha Carga", "presentation" => "DATETIME", 'classparams' => '{"force":true}', 'isvisible' => false, "actions" => "A"),
                array("name" => "codigo", "label" => "Código", 'note' => 'Ejemplo: IMP'),
                array("name" => "nombre", "label" => "Nombre")
            )
        )
    );
    
    public function add() {
        $this->maint = $this->maint_tipos;
        parent::add();
    }

    public function edit($id = null) {
        $this->maint = $this->maint_tipos;
        parent::edit($id);
    }

    public function index() {
        $this->search_list = $this->sl_tipos;
        parent::index();
    }

    public function view($id = null) {
        $this->maint = $this->maint_tipos;
        parent::view($id);
    }
    
    public function ajax_stock_disponible($id = null) {
        $this->layout = 'ajax';
        $this->Tipo->id = $id;
        if (!$this->Tipo->exists()) {
            throw new NotFoundException(__('Tipo inexistente'));
        }
        $query = $this->Tipo->query("SELECT Stock.id, Stock.producto FROM Stock
                                     WHERE Stock.tipo_id = '".$id."' AND Stock.cantidad > 0");
        $json = json_encode($query);
        $this->set('json', $json);
    }

}